package com.nimitz.module.drip.service.impl;

import com.nimitz.common.constant.OrderConstants;
import com.nimitz.common.utils.bean.BeanUtils;
import com.nimitz.module.drip.domain.DriBusiness;
import com.nimitz.module.drip.domain.DriDuty;
import com.nimitz.module.drip.mapper.DriDutyMapper;
import com.nimitz.module.drip.service.IDriBusinessService;
import com.nimitz.module.drip.service.IDriDutyService;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * 权责收入 业务层实现
 */
@Service
public class DriDutyServiceImpl implements IDriDutyService {

    private static final Logger logger = LoggerFactory.getLogger(DriDutyServiceImpl.class);

    @Resource
    private DriDutyMapper dutyMapper;

    @Resource
    private IDriBusinessService businessService;

    /**
     * 生成权责收入
     *
     * @param brandName 品牌名称
     * @param orderDate 订单日期
     * @return 结果
     */
    @Override
    @Transactional
    public int generateDuty(String brandName, String orderDate) {
        // 线程延时队列-开始
        ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1);
        executorService.schedule(() -> {
            try {
                // 日营业额中查找权责收入
                List<DriBusiness> businessList = businessService.selectBusinessListByOrderTypeAndOrderDate(brandName, OrderConstants.ORDER_TYPE_WRITEOFF, orderDate);
                if (CollectionUtils.isNotEmpty(businessList)) {
                    // 批量插入权责收入
                    this.insertDuty(brandName, businessList);
                    // 修改日营业额权责收入状态
                    this.updateBusinessByDutyStatus(brandName, businessList);
                }
            } catch (Exception e) {
                logger.info("生成权责收入" + e.getMessage());
            } finally {

            }
        }, 0, TimeUnit.SECONDS);
        // 线程延时队列-结束
        return 1;
    }

    /**
     * 新增权责收入
     *
     * @param brandName    品牌名称
     * @param businessList 日营业额集合
     * @return 结果
     */
    @Override
    public int insertDuty(String brandName, List<DriBusiness> businessList) {
        List<DriDuty> dutyList = new ArrayList<>();
        for (DriBusiness business : businessList) {
            DriDuty duty = new DriDuty();
            BeanUtils.copyProperties(business, duty);
            dutyList.add(duty);
        }
        return dutyMapper.insertDuty(brandName, dutyList);
    }

    /**
     * 修改日营业额权责收入状态
     *
     * @param brandName    品牌名称
     * @param businessList 日营业额集合
     * @return 结果
     */
    public int updateBusinessByDutyStatus(String brandName, List<DriBusiness> businessList) {
        List<Integer> businessIds = businessList.stream().map(DriBusiness::getBusinessId).collect(Collectors.toList());
        return businessService.updateBusinessByDutyStatus(brandName, businessIds);
    }
}
